package com.example.bookkeepingelf.business.activity;

import android.os.Bundle;

import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.example.bookkeepingelf.R;
import com.example.bookkeepingelf.base.utils.DateUtils;
import com.example.bookkeepingelf.business.adapter.HomeBillDayRecyclerViewAdapter;
import com.example.bookkeepingelf.business.bean.BillFlowDayVo;
import com.example.bookkeepingelf.business.listener.OnOffListener;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeContentFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeContentFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private View view;

    private RecyclerView recyclerView;

    private HomeBillDayRecyclerViewAdapter homeBillDayRecyclerViewAdapter;

    private OnOffListener onOffListener;

    private NestedScrollView nestedScrollView;

    public HomeContentFragment() {
        // Required empty public constructor
    }

    public HomeContentFragment(OnOffListener onOffListener) {
        this.onOffListener = onOffListener;
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeContentFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeContentFragment newInstance(String param1, String param2) {
        HomeContentFragment fragment = new HomeContentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        this.view = inflater.inflate(R.layout.fragment_home_content, container, false);
        this.recyclerView = this.view.findViewById(R.id.hc_bill_day_recycler_view);
        this.nestedScrollView = this.view.findViewById(R.id.nestedScrollView);
        // 初始化 RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        List<BillFlowDayVo> list = this.buildData();
        // 假设有一个名为 iconNames 的字符串数组包含了所有的图标名称
        this.homeBillDayRecyclerViewAdapter = new HomeBillDayRecyclerViewAdapter(getContext(), list); // 传递实际的数据源
        recyclerView.setAdapter(homeBillDayRecyclerViewAdapter);
        // 设置网格布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        //注册滑动监听器
        this.registerSlideListener();
        // 动态设置 View 的高度
        this.dynamicsSetViewHeight();

        return this.view;
    }

    /**
     * 动态设置 View 的高度
     */
    private void dynamicsSetViewHeight() {
        // 获取屏幕高度
        WindowManager windowManager = getActivity().getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int screenHeight = display.getHeight();

        // 计算四分之一的高度
        int quarterHeight = screenHeight / 4;

        // 找到 topTextContent 并设置高度
        View topTextContent = view.findViewById(R.id.topTextContent);
        ViewGroup.LayoutParams layoutParams = topTextContent.getLayoutParams();
        layoutParams.height = quarterHeight; // 设置高度为屏幕的四分之一
        topTextContent.setLayoutParams(layoutParams);
    }

    /**
     * 注册滑动监听器
     */
    private void registerSlideListener() {
        // 设置滑动监听器
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY > oldScrollY) {
                    Log.d("ScrollEvent", "向下滚动，当前位置：" + scrollY);
                    if (scrollY > 200) {
                        // 向下滚动，执行你想要的操作
                        onOffListener.off();
                    }

                } else if (scrollY < oldScrollY) {
                    Log.d("ScrollEvent", "向上滚动，当前位置：" + scrollY);
                    if (scrollY < 200) {
                        // 向上滚动，执行你想要的操作
                        onOffListener.on();
                    }
                }
                // 检查是否滚动到底部
                if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
                    Log.d("ScrollEvent", "已滚动到页面底部");
                }
            }
        });
    }

    private List<BillFlowDayVo> buildData() {
        List<BillFlowDayVo> list = new ArrayList<>();
        BillFlowDayVo billFlowDayVo1 = new BillFlowDayVo() {{
            this.setCreateDate(DateUtils.stringToDate("2025-01-01 23:23:23", DateUtils.DATE_TEMPLATE_1));
            this.setWeek(2);
            this.setWeekStr("星期二");
            this.setIncomeAmount(new BigDecimal(100));
            this.setExpensesAmount(new BigDecimal(100));
            List<String> billFlows = new ArrayList<>();
            billFlows.add("吃饭");
            billFlows.add("做车");
            billFlows.add("卖水果");
            billFlows.add("打车");
            billFlows.add("买书");
            this.setBillFlows(billFlows);

        }};
        BillFlowDayVo billFlowDayVo2 = new BillFlowDayVo() {{
            this.setCreateDate(DateUtils.stringToDate("2025-01-01 23:23:23", DateUtils.DATE_TEMPLATE_1));
            this.setWeek(2);
            this.setWeekStr("星期二");
            this.setIncomeAmount(new BigDecimal(100));
            this.setExpensesAmount(new BigDecimal(100));
            List<String> billFlows = new ArrayList<>();
            billFlows.add("吃饭");
            billFlows.add("做车");
            this.setBillFlows(billFlows);
        }};
        BillFlowDayVo billFlowDayVo3 = new BillFlowDayVo() {{
            this.setCreateDate(DateUtils.stringToDate("2025-01-01 23:23:23", DateUtils.DATE_TEMPLATE_1));
            this.setWeek(2);
            this.setWeekStr("星期二");
            this.setIncomeAmount(new BigDecimal(100));
            this.setExpensesAmount(new BigDecimal(100));
            List<String> billFlows = new ArrayList<>();
            billFlows.add("吃饭");
            this.setBillFlows(billFlows);
        }};
        BillFlowDayVo billFlowDayVo4 = new BillFlowDayVo() {{
            this.setCreateDate(DateUtils.stringToDate("2025-01-01 23:23:23", DateUtils.DATE_TEMPLATE_1));
            this.setWeek(2);
            this.setWeekStr("星期二");
            this.setIncomeAmount(new BigDecimal(100));
            this.setExpensesAmount(new BigDecimal(100));
            List<String> billFlows = new ArrayList<>();
            billFlows.add("做高铁");
            billFlows.add("做飞机");
            billFlows.add("买基金");
            billFlows.add("买手表");
            this.setBillFlows(billFlows);
        }};
        list.add(billFlowDayVo1);
        list.add(billFlowDayVo2);
        list.add(billFlowDayVo3);
        list.add(billFlowDayVo4);
        return list;
    }
}